package org.jiangwei.cmput696.entitylinking.tacindex;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public final class TacHelper {

	public static final String KEY_ID = "id";

	public static final String KEY_WIKI_TITLE = "wtitle";

	public static final String KEY_TAC_TITLE = "ttitle";

	public static final String KEY_TEXT = "text";

	public static final String KEY_FACT = "fact";

	public static final String KEY_TYPE = "type";

	public static final String SPLIT_FACT = "|";

	public static final String KEY_WIKI_TITLE_FOR_SEARCH = "wsearch";
	
	public static final String DEFAULT_INDEX_DIR = 
			"/largedata1/cmput696/students/jiangwei/luceneIndex/e2tac_new";

	public static String createFactJSON(NodeList factNodes) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < factNodes.getLength(); i++) {
			Node node = factNodes.item(i);
			addRecursiveFacts(sb, node);
		}
		return sb.toString();
	}

	private static void addRecursiveFacts(StringBuilder sb, Node node) {
		if (node == null || node.getNodeType() != Node.ELEMENT_NODE) {
			return;
		}
		NodeList childList = node.getChildNodes();
		if (childList == null)
			return;
		Node child = ((Node) childList.item(0));
		if (child == null)
			return;
		String val = child.getNodeValue();
		if (val != null && val.trim().length() > 0) {
			sb.append(val.trim());
			sb.append(SPLIT_FACT);
		}
	}
}
